package cn.netswap.jeegorilla.framework.security.web;

import cn.netswap.jeegorilla.framework.security.bean.ImageValidCode;
import cn.netswap.jeegorilla.framework.security.bean.SmsValidCode;
import cn.netswap.jeegorilla.framework.security.bean.ValidCode;
import cn.netswap.jeegorilla.framework.security.util.GorillaValidCodeUtil;
import cn.netswap.jeegorilla.framework.security.util.SmsGenerator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lgw
 * @version 1.0
 * @date 2020/3/19 10:34
 */
@RestController
@RequestMapping("gorillaSecurity")
public class GorillaSecurityController {

    Logger log = LoggerFactory.getLogger(GorillaSecurityController.class);

    //session策略
    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    //session的key
    public static final String IMAGECODE_SESSION_KEY = "SESSION_KEY_IMAGE_CODE";

    public static final String SMSCODE_SESSION_KEY = "SESSION_KEY_SMS_CODE";

    @Autowired
    private GorillaValidCodeUtil gorillaValidCodeUtil;

    @Autowired
    private SmsGenerator smsGenerator;

    /**
     * 产生一个验证码图片
     * @param request
     * @param response
     */
    @RequestMapping("/code/validCodeImage")
    public void validCodeImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ImageValidCode imageValidCode = gorillaValidCodeUtil.generatorGorillaImage();
        ValidCode imageValidCodeinSession = new ValidCode();
        //把文件流剔除  1放到redis占空间太大，2bufferedImage 没有实现 序列号接口
        imageValidCodeinSession.setCode(imageValidCode.getCode());
        imageValidCodeinSession.setDate(imageValidCode.getDate());
        imageValidCodeinSession.setValidTimes(imageValidCode.getValidTimes());
        sessionStrategy.setAttribute(new ServletWebRequest(request), IMAGECODE_SESSION_KEY, imageValidCodeinSession);
        ImageIO.write(imageValidCode.getImage(), "jpg", response.getOutputStream());
    }

    @RequestMapping("/code/smsValidCode")
    @ResponseBody
    public String createCode(HttpServletRequest request) {
        SmsValidCode smsValidCodeInSession  = smsGenerator.smsGenerator();
        smsValidCodeInSession.setCode(StringUtils.upperCase(smsValidCodeInSession.getCode()));//全转成大写
        sessionStrategy.setAttribute(new ServletWebRequest(request), SMSCODE_SESSION_KEY, smsValidCodeInSession);
        return smsValidCodeInSession.getCode();
    }


}
